<fieldset id="cadastroEtapa">
	<legend>Cadastro de Etapas</legend>

	<label>Identificador Sequencial</label>
	<input type="text" id="idEtapa" name="id" class="input-small" />

	<label for="nomeEtapa">Nome</label>
	<input type="text" id="nomeEtapa" class="input-xlarge" name="nome" />

	<div style="padding-top: 20px; padding-botton: 10px;">
		<button class="btn btn-primary btn-large" id="salvarButton">Salvar</button>
	</div>

</fieldset>

<script type="text/javascript">
	var etapa_id = null;

	$(document).ready(function(){
		$("#salvarButton").click(function(){
			var data = form.serialize('cadastroEtapa');
			
			if(etapa_id != null){
				data['id'] = etapa_id;
			}

			var url = "<?= base_url() ?>etapa/salvar";
			if(etapa_id)
				url = "<?= base_url() ?>etapa/atualizar";

			form.submit(base_url + 'etapa/salvar',
					    data,
					    'POST');
		});

		load();
	});

	function load(){
		var id = form.loadVar('id');

		if(!id)
			return;

		modal.carregando();

		$.ajax({
			url: base_url + 'etapa/show_info_json',
			data: {id: id},
			type: 'GET',
			success: function(result){
				var jsonResult = JSON.parse(result);
				var jsonResult = JSON.parse(result);
				$.each(jsonResult, function(v,i){
					if(v == 'id')
						etapa_id = i;

					$('[name=' + v + ']').val(i);
						
				});
				modal.encerrar();
			},
			error: function(){
				modal.message('Erro ao carregar informações');
			}
		});

	
}
</script>